package com.unknow.first.app.version.conntroller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.unknow.first.api.common.CommonPage;
import com.unknow.first.api.common.CommonParam;
import com.unknow.first.app.version.entity.AppVersion;
import com.unknow.first.app.version.service.AppVersionService;
import com.unknow.first.app.version.vo.AppVersionParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cloud.constant.CoreConstant;
import org.cloud.dimension.annotation.SystemResource;
import org.cloud.exception.BusinessException;
import org.cloud.logs.annotation.AuthLog;
import org.cloud.vo.CommonApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "后台管理-app版本管理", tags = "APP-版本管理(后台)")
@Validated
@RestController
@RequestMapping("/admin/appVersion")
@SystemResource(path = "/admin/appVersion", parentMenuCode = "system", parentMenuName = "系统设置")
@Slf4j
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class AdminAppVersionController {

    private final AppVersionService appVersionService;
//    private final GridFsTemplate gridFsTemplate;

//    @ApiIgnore
////    @RequestMapping(method = RequestMethod.GET, value = "/test")
//    public CommonApiResult<String> test() {
//        String remark = "版本1.1.1plist文件";
//        String fileAuthRange = "public";
//        String contentType = "application/octet-stream";
//        String suffix = ".plist";
//        String fileContent = "aaa";
//        String fileName = "1.1.1-version.plist";
//        ByteArrayInputStream stream = new ByteArrayInputStream(fileContent.getBytes());
//        final BasicDBObject doc = new BasicDBObject();
//        doc.append(MongoDBEnum.metadataContentTypeKey.value(), contentType);
//        doc.append(MongoDBEnum.metadataOwnerKey.value(), MongoDBEnum.defaultFileOwnerId.getLong());  //游客
//        doc.append(MongoDBEnum.metadataFilesSuffixFieldName.value(), suffix);
//        doc.put("remark", remark);
//        doc.put("fileAuthRange", fileAuthRange);
//        String s = gridFsTemplate.store(stream, fileName, doc).toString();
//        return CommonApiResult.createSuccessResult(s);
//    }

    @ApiOperation(value = "管理员获取APP版本列表")
    @ApiImplicitParams({@ApiImplicitParam(value = "1安卓；2ios", name = "type", paramType = "query")})
    @SystemResource(value = "/admin-list", description = "管理员获取APP版本列表", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION, menuCode = "system/app/version", menuName = "app版本管理")
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public CommonApiResult<CommonPage<AppVersion>> list(String version, Integer type, String appName, @Validated CommonParam param) {
        LambdaQueryWrapper<AppVersion> lambdaQueryWrapper = Wrappers.lambdaQuery(AppVersion.class)
            .eq(!ObjectUtils.isEmpty(version), AppVersion::getVersion, version)
            .eq(type != null, AppVersion::getType, type)
            .eq(!StrUtil.isEmpty(appName), AppVersion::getAppName, appName);
        PageHelper.startPage(param.getPage(), param.getLimit(), param.getSorts());
        List<AppVersion> list = appVersionService.list(lambdaQueryWrapper);
        return CommonApiResult.createSuccessResult(CommonPage.restPage(list));
    }

    @ApiOperation(value = "管理员根据id获取app版本信息")
    @SystemResource(value = "/admin-get", description = "管理员根据id获取版本信息", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.GET, value = "/get")
    public CommonApiResult<AppVersion> get(@NotNull Integer id) {
        return CommonApiResult.createSuccessResult(appVersionService.getById(id));
    }

    @AuthLog(bizType = "app_version", desc = "添加App版本")
    @ApiOperation(value = "添加App版本")
    @SystemResource(value = "/admin-addVersion", description = "添加App版本", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/addVersion")
    public CommonApiResult<Boolean> addVersion(@Validated @RequestBody AppVersionParamVO vo) throws BusinessException {
        appVersionService.addVersion(vo);
        return CommonApiResult.createSuccessResult(true);
    }

    @AuthLog(bizType = "app_version", desc = "修改App版本信息")
    @ApiOperation(value = "修改App版本信息")
    @SystemResource(value = "/admin-updateVersion", description = "修改App版本信息", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/updateVersion")
    public CommonApiResult<Boolean> updateVersion(@Validated @RequestBody AppVersionParamVO vo) throws BusinessException {
        appVersionService.updateVersion(vo);
        return CommonApiResult.createSuccessResult(true);
    }
}
